package ai;
import java.awt.Point;
import java.util.Random;

public class Game
{
	public static final int	N		= 6;						//栅格边长
	public static final int	K		= 5;						//水滴的爆炸极限，达到该值时发生爆炸
	public static int [][]	state	= new int[Game.N][Game.N];
	public static int		water	= 10;
	
//	public static void allCase(int length)
//	{
//		Game.allCase("", length);
//	}
//	public static void allCase(String firstLetter, int length)
//	{
//		if(length == 0)
//		{
//			System.out.println(firstLetter);
//			return;
//		}
//		char [] letter = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
//		for(int i = 0; i < letter.length; i++)
//		{
//			Game.allCase(firstLetter + letter[i], length - 1);
//		}
//	}
	public static void allCase(Point [] action, int length)
	{
		if(length == 0)
		{
			int game[][] = {{0, 0, 2, 4, 1, 3 }, {2, 4, 3, 2, 4, 4 }, {2, 1, 1, 0, 3, 3 }, {3, 0, 1, 3, 0, 3 }, {0, 1, 2, 0, 2, 2 }, {3, 3, 1, 2, 4, 2 } };
			Game.state = game;
			Game.water = 10;
			int iniWater = Game.water;
			int iniWaterNum = Game.checkWater();
//			System.out.println("原有水滴数：" + Game.water);
//			System.out.println("原水滴分布：");
//			Game.display(Game.state);
//			System.out.println("");
			for(int i = 0; i < action.length; i++)
			{
				int combo = Game.drop(action[i].x, action[i].y, 0, 0); //点击该栅格
				if(combo != 0)
				{
					Game.water += combo / 3;
					combo = 0;
				}
			}
			int curWater = Game.water;
			int curWaterNum = Game.checkWater();
			if(((curWater - iniWater) >= 2) && ((iniWaterNum - curWaterNum) >= 25))
			{
				System.out.print("增:" + (curWater - iniWater));
				System.out.print("消：" + (iniWaterNum - curWaterNum) + " \t");
//				System.out.print("  原水数：" + iniWaterNum + "  现水数：" + curWaterNum + "  消去数：" + (iniWaterNum - curWaterNum));
				if(new Random().nextInt(100) > 95)
				{
					System.out.println();
				}
//				System.out.print(iniWater + "-" + curWater);
//				for(int i = 0; i < action.length; i++)
//				{
//					System.out.print("   (" + action[i].x + "," + action[i].y + ")  ");
//				}
//				System.out.println();
			}
//			try
//			{
//				Thread.sleep(100);
//			}catch(InterruptedException e)
//			{
//				// TODO 自动生成的 catch 块
//				e.printStackTrace();
//			}
//			System.out.println("现有水滴数：" + Game.water);
//			System.out.println("现水滴分布：");
//			Game.display(Game.state);
			return;
		}
		for(int i = 0; i < Game.K; i++)
		{
			for(int j = 0; j < Game.K; j++)
			{
				action[length - 1] = new Point(i, j);
				Game.allCase(action, length - 1);
			}
		}
	}
	
	public static int checkWater()
	{
		int num = 0;
		for(int i = 0; i < Game.state.length; i++)
		{
			for(int j = 0; j < Game.state[i].length; j++)
			{
				if(Game.state[i][j] != 0)
				{
					num++;
				}
			}
		}
		return num;
	}
	
	//显示水滴分布
	public static void display(int game[][])
	{
		for(int i = 0; i < Game.N; i++)
		{
			for(int j = 0; j < Game.N; j++)
			{
				System.out.print(game[i][j] + "\t");
			}
			System.out.println();
			System.out.println();
		}
	}
	
	/************************************************************************
	 * 加水滴
	 * (x,y)为欲加入水滴的栅格的坐标
	 * k: 水滴破裂时所含有的水滴数量
	 * m: 坛子里的水滴数量
	 * op为水滴操作
	 * 0:添加水滴
	 * 1:释放的水滴向左运动
	 * 2:释放的水滴向右运动
	 * 3:释放的水滴向上运动
	 * 4:释放的水滴向下运动
	 * curCombo:连击水滴数
	 ************************************************************************/
	public static int drop(int x, int y, int op, int curCombo)
	{
		if((x < 0) || (y < 0) || (x > (Game.N - 1)) || (y > (Game.N - 1)))
		{
			return curCombo;
		}
		if(op == 0) //点击该栅格加入水滴
		{
			Game.water--; //该水滴为坛子中的水滴
			curCombo = 0;
			if(Game.state[x][y] < Game.K)
			{
				Game.state[x][y]++;
			}
		}else
		//表示加入的水滴为破裂的水滴释放的水滴
		{
			if(Game.state[x][y] == 0) //该水滴按照原来的方向继续移动
			{
				if(op == 1)
				{
					curCombo = Game.drop(x - 1, y, 1, curCombo); //go on moving to left
				}else if(op == 2)
				{
					curCombo = Game.drop(x + 1, y, 2, curCombo); //go on moving to right
				}else if(op == 3)
				{
					curCombo = Game.drop(x, y - 1, 3, curCombo); //go on moving to up
				}else if(op == 4)
				{
					curCombo = Game.drop(x, y + 1, 4, curCombo); //go on moving to down
				}
			}else if(Game.state[x][y] < Game.K)
			{
				Game.state[x][y]++;
			}
		}
		//若水滴满,则破裂,并释放水滴
		if(Game.state[x][y] == Game.K)
		{
			Game.state[x][y] = 0; //水滴破裂
			curCombo += 1;
			curCombo = Game.drop(x - 1, y, 1, curCombo); //release a drop moving to left
			curCombo = Game.drop(x + 1, y, 2, curCombo); //release a drop moving to right
			curCombo = Game.drop(x, y - 1, 3, curCombo); //release a drop moving to up
			curCombo = Game.drop(x, y + 1, 4, curCombo); //release a drop moving to down
		}
		return curCombo;
	}
	
	public static void main(String [] args)
	{
//		Game.allCase(new Point[5], 5);
		System.out.println("原有水滴数：" + Game.checkWater());
		Game.smallCase(7);
	}
	
	public static void smallCase(int length)
	{
		for(int i = 1; i <= length; i++)
		{
			Game.allCase(new Point[i], i);
		}
	}
}
